<?php
namespace Factory\Controller;
use Mcit\Page;

class IndexController extends BaseController {

    public function index(){
//        P($_SESSION);
        //2---厂商  3---运营商
        if(session("fac_type") == 2){
            if (session('fac_key')){
                $mapa = "a.ev_fac_code='".session('fac_key')."' and b.yy_user_id='".session('uid')."'";
                //充电总次数
                $this->sumCharge  = M("fac_bill_total")->where("fac_id='".session('uid')."' and state=1")->sum('total');
                //预约总次数
                $this->sumBespeak = M("fac_bespeak_total")->where("fac_id='".session('uid')."' and state=1")->sum("total");
                //碳排放总量
                $countCarbon      = M("fac_bill_total")->field("SUM(electricity) as electricity")->where("fac_id='".session('uid')."' and state=1")->select();
                foreach($countCarbon as $k => $v){
                    $this->sumCarbon  = sprintf("%.2f", $v['electricity'] * 1.174);
                }
            }else{
                $mapa = "a.ev_fac_code='".session('fac_login_name')."'";
                //充电总次数
                $this->sumCharge  = M("fac_bill_total")->where("fac_id='".session('uid')."'  OR fac_pid='".session('uid')."'  and state=1")->sum('total');
                //预约总次数
                $this->sumBespeak = M("fac_bespeak_total")->where("fac_id='".session('uid')."'  OR fac_pid='".session('uid')."' and state=1")->sum("total");
                //碳排放总量
                $countCarbon      = M("fac_bill_total")->field("SUM(electricity) as electricity")->where("fac_id='".session('uid')."' OR fac_pid='".session('uid')."' and state=1")->select();
                foreach($countCarbon as $k => $v){
                    $this->sumCarbon  = sprintf("%.2f", $v['electricity'] * 1.174);
                }
            }
        }elseif(session("fac_type") == 3){
            if(session('pid') != 0){
                $mapa = "b.plant_attr=3 and b.yy_user_id='".session('uid')."'";
                //充电总次数
                $this->sumCharge  = M("oper_bill_total")->where("oper_id='".session('uid')."' and state=1")->sum('total');
                //预约总次数
                $this->sumBespeak = M("oper_bespeak_total")->where("oper_id='".session('uid')."' and state=1")->sum("total");
                //碳排放总量
                $countCarbon      = M("oper_bill_total")->field("SUM(electricity) as electricity")->where("oper_id='".session('uid')."' and state=1")->select();
                foreach($countCarbon as $k => $v){
                    $this->sumCarbon  = sprintf("%.2f", $v['electricity'] * 1.174);
                }
            }else{
                $yyuserid = M()->query("select id from ev_fac where id='".session('uid')."' OR pid='".session('uid')."'");
                foreach($yyuserid as $k=>$v){
                    $yyuserids .= $v['id'].",";
                }
                $yyuserids = trim($yyuserids,',');
                $mapa      = "b.plant_attr=3 and b.yy_user_id IN($yyuserids)";
                //充电总次数
                $this->sumCharge  = M("oper_bill_total")->where("oper_id='".session('uid')."'  OR oper_pid='".session('uid')."'  and state=1")->sum('total');
                //预约总次数
                $this->sumBespeak = M("oper_bespeak_total")->where("oper_id='".session('uid')."'  OR oper_pid='".session('uid')."' and state=1")->sum("total");
                //碳排放总量
                $countCarbon      = M("oper_bill_total")->field("SUM(electricity) as electricity")->where("oper_id='".session('uid')."' OR oper_pid='".session('uid')."' and state=1")->select();
                foreach($countCarbon as $k => $v){
                    $this->sumCarbon  = sprintf("%.2f", $v['electricity'] * 1.174);
                }
            }
        }


        $sql  = "select * from ev_char a left join ev_plant b on b.id=a.ev_plant_id where {$mapa}";
        $char = M()->query($sql);

        $key_links = '';
        foreach($char as $k => $v){
            $key_links .= $v['key_link'] .",";
        }
        $map['key_link'] = array('IN',rtrim($key_links,','));
        $this->charAll   = D("Admin/State")->where($map)->count();//当前厂商或运营商下的电桩状态总数

        $map['charge']   = 1;
        $this->charge    = D("Admin/State")->where($map)->count();//充电中

        $map['charge']   = 0;
        $map['bespeak']  = 0;
        $map['error']    = 0;
        $map['cable']    = 0;
        $this->release   = D("Admin/State")->where($map)->count();//空闲

        $Map['key_link'] = array('IN',rtrim($key_links,','));
        $Map['error']    = 1;
        $this->error     = D("Admin/State")->where($Map)->count();//故障下线

        $this->display();
	}

    //充电总数以及预约总数
    public function chart(){
        if(IS_AJAX){
            //2---厂商  3---运营商
            if(session("fac_type") == 2){
                if (session('fac_key')){
                    //充电数据最近7天统计
                    $countCharge   = M("fac_bill_total")->field("SUM(total) as total,currentTime")->where("fac_id='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //预约数据最近7天统计
                    $countBespeak  = M("fac_bespeak_total")->field("SUM(total) as total,currentTime")->where("fac_id='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //碳排放总量近7天统计
                    $countCarbon   = M("fac_bill_total")->field("SUM(electricity) as electricity,currentTime")->where("fac_id='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    foreach($countCarbon as $k => $v){
                        $countCarbon[$k]['carbon']  = sprintf("%.2f", $v['electricity'] * 1.174);
                    }

                }else{
                    //充电数据最近7天统计
                    $countCharge  = M("fac_bill_total")->field("SUM(total) as total,currentTime")->where("fac_id='".session('uid')."' OR fac_pid='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //预约数据最近7天统计
                    $countBespeak = M("fac_bespeak_total")->field("SUM(total) as total,currentTime")->where("fac_id='".session('uid')."' OR fac_pid='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //碳排放总量近7天统计
                    $countCarbon  = M("fac_bill_total")->field("SUM(electricity) as electricity,currentTime")->where("fac_id='".session('uid')."' OR fac_pid='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    foreach($countCarbon as $k => $v){
                        $countCarbon[$k]['carbon']  = sprintf("%.2f", $v['electricity'] * 1.174);
                    }
                }
            }elseif(session("fac_type") == 3){
                if(session('pid') != 0){
                    //充电数据最近7天统计
                    $countCharge  = M("oper_bill_total")->field("SUM(total) as total,currentTime")->where("oper_id='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //预约数据最近7天统计
                    $countBespeak = M("oper_bespeak_total")->field("SUM(total) as total,currentTime")->where("oper_id='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //碳排放总量近7天统计
                    $countCarbon  = M("oper_bill_total")->field("SUM(electricity) as electricity,currentTime")->where("oper_id='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    foreach($countCarbon as $k => $v){
                        $countCarbon[$k]['electricity']  = sprintf("%.2f", $v['electricity'] * 1.174);
                    }
                }else{
                    //充电数据最近7天统计
                    $countCharge  = M("oper_bill_total")->field("SUM(total) as total,currentTime")->where("oper_id='".session('uid')."' OR oper_pid='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //预约数据最近7天统计
                    $countBespeak = M("oper_bespeak_total")->field("SUM(total) as total,currentTime")->where("oper_id='".session('uid')."' OR oper_pid='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    //碳排放总量近7天统计
                    $countCarbon  = M("oper_bill_total")->field("SUM(electricity) as electricity,currentTime")->where("oper_id='".session('uid')."' OR oper_pid='".session('uid')."' and state=1")->group("currentTime")->order("currentTime desc")->limit(7)->select();
                    foreach($countCarbon as $k => $v){
                        $countCarbon[$k]['carbon']  = sprintf("%.2f", $v['electricity'] * 1.174);
                    }
                }
            }
            if($_POST['type'] == "charge"){
                //最近7天充电总次数
                echo json_encode($countCharge);
            }elseif($_POST['type'] == "bespeak"){
                //最近7天总预约数
                echo json_encode($countBespeak);
            }elseif($_POST['type'] == "carbon"){
                //最近7天碳排放
                echo json_encode($countCarbon);
            }
        }
    }
}